import React, { memo, useState, useCallback } from 'react';
import { Button, message, Modal, Icon } from 'antd';

import { isSuccess } from '@/utils/request';

import { hrOverviewRecheck } from '@/services/salaryManage/salaryCalculate';

const { finalPassCheck } = hrOverviewRecheck;

const { confirm } = Modal;

// 复核终审
const PassFinalCheck = memo(({ handleItem, year, month, organizationCode, actionType, updateStateFn }) => {
     //  弹窗=》确定按钮是否加载状态
    const [confirmLoad, setConfirmLoad] = useState(false);

    // 操作按钮
    const handle = useCallback(async () => {
        confirm({
            title: '终审通过确认',
            content: '确认全部薪资终审通过吗？',
            icon: <Icon type="exclamation-circle" theme="filled" />,
            confirmLoading: confirmLoad,
            centered: true,
            onOk: async () => {
                setConfirmLoad(true);
                const payoffRes = await finalPassCheck({
                    year,
                    month,
                    organizationCode,
                    actionType
                })

                if (isSuccess(payoffRes)) {
                    updateStateFn(payoffRes)
                } else {
                    message.error(payoffRes.msg)
                }
                setConfirmLoad(false);
            },
            onCancel() {}
        });
    }, [actionType, confirmLoad, month, organizationCode, updateStateFn, year]);


    return (
        <Button type="primary" key={handleItem.code} onClick={handle}>
            {handleItem.name}
        </Button>
    );
});

export default memo(PassFinalCheck)
